﻿using PropertyEntities.RequestEntities;
using PropertyEntities.ResponseEntities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PropertyDataContext.UnitOfWork;
using PropertyDataContext.Model;

namespace PropertyDAL
{
    public class AdminDAL
    {
        UOWEntities uow = new UOWEntities();

        public AdminResponse RegisterAdmin(AdminRequest adminRequest)
        {
            AdminResponse adminReponse = new AdminResponse();

            try
            {
                bool isUsernameExist = uow.adminRepositery.Get().Any(ad => ad.Username == adminRequest.Username);
                bool isEmailExist = uow.adminRepositery.Get().Any(ad => ad.Email == adminRequest.Email);
                if (isEmailExist == false)
                {
                    if (isUsernameExist == false)
                    {
                        Admin admin = new Admin();
                        admin.Address1 = adminRequest.Address1;
                        admin.Address2 = adminRequest.Address2;
                        admin.City = adminRequest.City;
                        admin.Country = adminRequest.Country;
                        admin.Email = adminRequest.Email;
                        admin.Name = adminRequest.Name;
                        admin.Password = adminRequest.Password;
                        admin.Phoneno = adminRequest.Phoneno;
                        admin.Username = adminRequest.Username;
                        uow.adminRepositery.Insert(admin);
                        uow.Save();
                        adminReponse.SuccessMessage = "Admin Registered Successfully";
                        adminReponse.IsResult = true;
                    }
                    else
                    {
                        adminReponse.ErrorMessage = "Username already exist.";
                        adminReponse.IsResult = false;
                    }
                }
                else
                {
                    adminReponse.ErrorMessage = "Email address already exist.";
                    adminReponse.IsResult = false;
                }
            }
            catch
            {
                adminReponse.ErrorMessage = "Some error occured.";
                adminReponse.IsResult = false;
            }

            return adminReponse;
        }

        public AdminResponse DeleteAgent(string AgentId)
        {
            AdminResponse adminResponse = new AdminResponse();

            try
            {
                Agent agent = uow.agentRepositery.Get().Where(ad => ad.AgentID == Convert.ToInt32(AgentId)).FirstOrDefault();
                if (agent != null)
                {
                    uow.agentRepositery.Delete(AgentId);
                    adminResponse.SuccessMessage = "Agent Deleted Successfully";
                    adminResponse.IsResult = true;
                }
                else
                {
                    adminResponse.ErrorMessage = "Agent doesn't exist.";
                    adminResponse.IsResult = false;
                }
            }
            catch
            {
                adminResponse.ErrorMessage = "Some error occured.";
                adminResponse.IsResult = false;
            }
            return adminResponse;
        }

        public AdminResponse UpdateAdmin(AdminRequest adminRequest)
        {
            AdminResponse adminResponse = new AdminResponse();
            try
            {
                bool isUsernameExist = uow.adminRepositery.Get().Any(ad => ad.Username == adminRequest.Username);
                bool isEmailExist = uow.adminRepositery.Get().Any(ad => ad.Email == adminRequest.Email);
                if (isEmailExist == false)
                {
                    if (isUsernameExist == false)
                    {
                        Admin admin = uow.adminRepositery.Get().Where(ad => ad.AdminID == adminRequest.AdminID).FirstOrDefault();
                        admin.Address1 = adminRequest.Address1;
                        admin.Address2 = adminRequest.Address2;
                        admin.City = adminRequest.City;
                        admin.ContactNo = adminRequest.ContactNo;
                        admin.Country = adminRequest.Country;
                        admin.Email = adminRequest.Email;
                        admin.Name = adminRequest.Name;
                        admin.Phoneno = adminRequest.Phoneno;
                        admin.Username = adminRequest.Username;
                        uow.adminRepositery.Update(admin);
                        uow.Save();
                        adminResponse.IsResult = true;
                        adminResponse.SuccessMessage = "Admin Updated Successfully.";
                        adminResponse.AdminID = admin.AdminID;
                    }
                    else
                    {
                        adminResponse.ErrorMessage = "Username already exist.";
                        adminResponse.IsResult = false;
                    }
                }
                else
                {
                    adminResponse.ErrorMessage = "Email address already exist.";
                    adminResponse.IsResult = false;
                }
            }
            catch
            {
                adminResponse.ErrorMessage = "Some error occured.";
                adminResponse.IsResult = false;
            }
            return adminResponse;
        }
    }
}
